//
//  KOPClient.m
//  KOP
//
//  Created by 王贵源 on 15/9/23.
//  Copyright © 2015年 王贵源. All rights reserved.
//

#import "KOPClient.h"
#import "SignUtils.h"
#import  "DGJDescrypt.h"
#import "FilePostSender.h"
#import   "PostSender.h"

@interface KOPClient()<PostSenderDelegate,FilePostSenderDelegate>

@end

@implementation KOPClient{
    NSMutableDictionary *_resultDic;
}

+ (instancetype)sharedInstance {
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] initWithKopandappKey:@"" andSK:@"" andendPoint:@"http://10.0.1.114:8080/service"];//@"http://120.25.128.223/ares/service"];//线上////@"http://10.0.1.236:8080/ROOT/service"];//@"http://10.0.1.236:8080/ROOT/service"];//@"http://10.0.1.236:8080/app/service"];//:
    });
    
    return sharedInstance;
}

- (NSMutableDictionary *)sendKopRequest:(NSString *)method data:(NSMutableDictionary *)data{
    return [self sendKopRequest:method data:data fileData:nil];
}


- (KOPClient *)initWithKopandappKey:(NSString *)appkey andSK:(NSString *)sk andendPoint:(NSString *)endPoint{
    self = [super init];
    if (self) {
        _appkey = appkey;
        _sk = sk;
        _endpoint = endPoint;
    }
    return self;
    
}

- (NSMutableDictionary *)sendKopRequest:(NSString *)method data:(NSMutableDictionary *)data fileData: (NSMutableDictionary *) fileData{
    
    /***
     
     1.做签名
     
     
     
     2.拼装 KOP 协议的 header
     
     放入 uuid cookie
     
     跟签名相关的信息
     
     method
     ak
     sk
     length
     
     
     3.用 postsender 发送请求
     4.解密响应内容
     5.处理 kop 协议相关的数据
     
     response 对象上面去取 header ----> 在 header 里面取 set-cookie
     
     
     */
    int length = 0;
    if (data) {
        length += [data count];
    }
    if (fileData) {
        length += [fileData count];
    }
    [data setValue:method forKey:@"method"];
    
    NSString * sign = [SignUtils signMethod:method ak:_appkey sk:_sk length:length];
    
    NSMutableDictionary * header = [[NSMutableDictionary alloc] init];
    
    if (_uuid) {
        [header setValue:_uuid forKey:@"uuid"];
    }
    if (_cookie) {
        [header setValue:_cookie forKey:@"cookie"];
    }
    
    [header setValue:_appkey forKey:@"ak"];
    [header setValue:@"ios" forKey:@"client"];
    [header setValue:_sk forKey:@"sk"];
    [header setValue:sign forKey:@"sign"];
    [header setValue:method forKey:@"method"];
    [header setValue:[NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]*1000] forKey:@"timestamp"];
    [header setValue:[NSString stringWithFormat:@"%i",length] forKey:@"length"];
    
    //    NSMutableDictionary * resp;
    
    if (fileData) {
        FilePostSender * sender = [[FilePostSender alloc]init];
        //        resp =
        [sender sendTo:_endpoint data:data fileData:fileData header:header];
        sender.filepostDelegate = self;
        
    }else{
        PostSender * sender = [[PostSender alloc] init];
        // resp =
        sender.postDelegate = self;
        [sender sendTo:_endpoint data:data header:header];
    }
    
    //    NSDictionary * _headers = [resp objectForKey:@"headers"];
    //    _uuid = [_headers objectForKey:@"uuid"];
    //    _cookie = [_headers objectForKey:@"Set-Cookie"];
    //
    //
    //    NSString* result = [resp objectForKey:@"result"];
    //
    //    //解密 ----> json 解析作为返回值
    //
    //    NSData * _result = [DGJDescrypt decryptUseDES:result key:@"abcdefgh"];
    //
    //    NSError * error = nil;
    //
    //    id _result_ = [NSJSONSerialization JSONObjectWithData:_result options:NSJSONReadingMutableContainers error:&error];
    //    if (!error) {
    //        return _result_;
    //    }
    NSDictionary * _headers = [_resultDic objectForKey:@"headers"];
    _uuid = [_headers objectForKey:@"uuid"];
    _cookie = [_headers objectForKey:@"Set-Cookie"];
    
    
    NSString* result = [_resultDic objectForKey:@"result"];
    
    //解密 ----> json 解析作为返回值
    
    NSData * _result = [DGJDescrypt decryptUseDES:result key:@"abcdefgh"];
    
    NSError * error = nil;
    
    id _result_ = [NSJSONSerialization JSONObjectWithData:_result options:NSJSONReadingMutableContainers error:&error];
    if (!error) {
        return _result_;
    }
    return nil;
}

#pragma mark - PostSenderDelegate
- (void)sendresultDic:(NSMutableDictionary *)resultDic{
    _resultDic = resultDic;
}
#pragma mark - FilePostSenderDelegate
-(void)filesendresultDic:(NSMutableDictionary *)resultDic{
    _resultDic = resultDic;
}
@end
